package com.tcc.login.logic;


import org.apache.xpath.operations.String;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;

import com.tcc.qa.test.util.BaseTest;
import com.tcc.qa.test.util.FormFinder;
import com.tcc.qa.test.util.InputFinder;

public class Login {
	public static WebDriver testLogin(WebDriver driver,String userName,String passWord) throws InterruptedException {
		driver.get(BaseTest.testDomainURL + "q?pg=home_welcome&cp=1");
		Thread.sleep(3000);
		WebDriverWait wait = new WebDriverWait(driver, 10);
		wait.until(new ExpectedCondition<WebElement>() {
			@Override 
			public WebElement apply(WebDriver d) {
				return InputFinder.getInputById(d, "userName");
			}
		}).sendKeys(userName);
		wait.until(new ExpectedCondition<WebElement>() {
			@Override
			public WebElement apply(WebDriver d) {
				return InputFinder.getInputById(d, "password");
			}
		}).sendKeys(passWord);
		wait.until(new ExpectedCondition<WebElement>() {
			@Override
			public WebElement apply(WebDriver d) {
				return FormFinder.getForm(d, "loginEntry");
			}
		}).submit();
		for (String path : driver.getWindowHandles()) {
			driver.switchTo().window(path);
		}
		if (driver.getCurrentUrl().startsWith(
				BaseTest.testDomainURL + "/q?pg=loginRedirect&cp=1&rqid=")) {
			wait.until(new ExpectedCondition<WebElement>() {
				@Override
				public WebElement apply(WebDriver d) {
					return d.findElement(By.linkText("Continue ..."));
				}
			}).click();
		}

		return driver;

	}
}
